#include "../main.h"

int encode_msg(int sockfd, char *msg, int msg_size)
{
    usleep(1000);
    return send_file_msg(sockfd, msg, msg_size, NULL, 0);
}

int encode_file_msg(int sockfd, char *msg, int msg_size, char *data, int data_size)
{
    char *buffer = NULL;
    if (data_size > 0)
    {
        if (data != NULL)
        {
            buffer = (char *)malloc(data_size);
            memcpy(buffer, data, data_size);
        }
    }
    int ret = send_file_msg(sockfd, msg, msg_size, buffer, data_size);
    return ret;
}

int send_file_msg(int sockfd, char *msg, int msg_size, char *data, int data_size)
{
    int total_size = msg_size + data_size;
    char *content = (char *)malloc(total_size + 6 * sizeof(int));
    memset(content, 0, total_size);
    int Msg_Head = htonl(MsgHead);
    int Msg_Ver = htonl(MsgVer);
    int File_Type = htonl(FileType);
    int msg_length = htonl(msg_size);
    int data_length = htonl(data_size);
    int offset = 0;
    memcpy(content, &Msg_Head, sizeof(int));
    offset += 4;
    memcpy(content + offset, &Msg_Ver, sizeof(int));
    offset += 4;
    memcpy(content + offset, &File_Type, sizeof(int));
    offset += 4;
    int total_length = htonl(total_size);
    memcpy(content + offset, &total_length, sizeof(int));
    offset += 4;
    memcpy(content + offset, &msg_length, sizeof(int));
    offset += 4;
    memcpy(content + offset, &data_length, sizeof(int));
    offset += 4;
    memcpy(content + offset, msg, msg_size);
    offset += msg_size;
    if (data_size > 0)
    {
        memcpy(content + offset, data, data_size);
        offset += data_size;
        free(data);
        data = NULL;
    }

    int ret = tcp_socket_write(sockfd, content, offset);

    free(content);
    content = NULL;
    return ret;
}

int tcp_socket_write(int fd, void *buf, int nSize)
{
    int wleft = nSize;
    int wlen;
    char *ptr = (char *)buf;

    int i = 0;
    int max = 10;
    while (wleft > 0)
    {
        if ((wlen = send(fd, ptr, wleft, 0)) <= 0)
        {
            if (wlen < 0)
            {
                OUT_DEBUG(DEBUG, "errno:%d", errno);
                if (errno == EINTR)
                {
                    OUT_DEBUG(DEBUG, "errno == EINTR");
                    close(fd);
                    return -1;
                }
                if (errno == EWOULDBLOCK)
                {
                    OUT_DEBUG(DEBUG, "errno == EWOULDBLOCK");
                }
                if (errno == EAGAIN)
                {
                    OUT_DEBUG(DEBUG, "errno == EAGAIN");
                }
                i++;
                OUT_DEBUG(DEBUG, "try times:%d", i);
                wlen = 0;
                sleep(1);
            }
        }
        if (i >= max)
        {
            OUT_DEBUG(DEBUG, "try times:%d", i);
            break;
        }
        wleft -= wlen;
        ptr += wlen;
        usleep(1000);
        // OUT_DEBUG(DEBUG, "send wlen %d,left size %d", wlen, wleft);
    }

    // OUT_DEBUG(DEBUG, "a402");
    if (wleft > 0)
    {
        OUT_DEBUG(DEBUG, "l size %d", wleft);
        return -1;
    }
    else
    {
        return 0;
    }
}
